package com.csoa.community.utils;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;

public class LocationUtil {
	public static boolean canSupportGPS(Context ctx) {
		LocationManager manager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
		boolean b = manager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)
				|| manager.isProviderEnabled(android.location.LocationManager.NETWORK_PROVIDER);
		return b;
	}

	/**
	 * 
	 * @return 经纬度数组，下标0是经度，下标1是纬度
	 */
	public static String[] getLongtitude_Latitude(Context ctx) {
		LocationManager manager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		// 查询精度：高
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		// 是否查询海拨：否
		criteria.setAltitudeRequired(false);
		// 是否查询方位角 : 否
		criteria.setBearingRequired(false);
		// 是否允许付费：是
		criteria.setCostAllowed(true);
		// 电量要求：低
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		// 返回最合适的符合条件的 provider ，第 2 个参数为 true 说明 , 如果只有一个 provider 是有效的 , 则返回当前
		// provider
		String provider = manager.getBestProvider(criteria, true);
		Location location = manager.getLastKnownLocation(provider);
		if (location == null) {
			location = manager.getLastKnownLocation(android.location.LocationManager.PASSIVE_PROVIDER);
		}
		if (location != null) {
			return new String[] { String.valueOf(location.getLongitude()), String.valueOf(location.getLatitude()) };
		}
		return null;
	}
}
